package cn.edu.scnu.impl;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.entity.MovieDetails;
import cn.edu.scnu.entity.MovieStats;
import cn.edu.scnu.mapper.MovieMapper;
import cn.edu.scnu.mapper.MovieStatsMapper;
import cn.edu.scnu.service.MovieRankService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MovieRankServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieRankService {

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private MovieStatsMapper movieStatsMapper;

    @Override
    public Page<MovieDetails> getMovieDetailsPage(int page, int size) {
        Page<Movie> moviePage = new Page<>(page, size);
        movieMapper.selectPage(moviePage, null);

        List<MovieDetails> movieDetailsList = moviePage.getRecords().stream().map(movie -> {
            MovieStats movieStats = movieStatsMapper.selectById(movie.getMovieId());
            MovieDetails movieDetails = new MovieDetails();
            movieDetails.setMovie(movie);
            movieDetails.setMovieStats(movieStats);
            return movieDetails;
        }).collect(Collectors.toList());

        Page<MovieDetails> resultPage = new Page<>(page, size, moviePage.getTotal());
        resultPage.setRecords(movieDetailsList);
        return resultPage;
    }

    /**
     * 当周排行
     * @param page
     * @param size
     * @return
     */

    @Override
    public Page<MovieDetails> getWeeklyRankPage(int page, int size) {
        return getRankPage(page, size, "weekly_views");
    }

    /**
     * 每月排行
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<MovieDetails> getMonthlyRankPage(int page, int size) {
        return getRankPage(page, size, "monthly_views");
    }

    /**
     * 好评排行
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<MovieDetails> getLikesRankPage(int page, int size) {
        return getRankPage(page, size, "likes");
    }


    /**
     * 热门排行
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<MovieDetails> getHotRankPage(int page, int size) {
        return getRankPage(page, size, "hot_rank");
    }

    /**
     * 总排行
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<MovieDetails> getTotalRankPage(int page, int size) {
        return getRankPage(page, size, "total_rank");
    }

    /**
     * 获取分页
     * @param page
     * @param size
     * @param orderBy
     * @return
     */
    private Page<MovieDetails> getRankPage(int page, int size, String orderBy) {
        Page<MovieStats> movieStatsPage = new Page<>(page, size);
        movieStatsPage.setOrders(List.of(new com.baomidou.mybatisplus.core.metadata.OrderItem().setColumn(orderBy).setAsc(false)));
        movieStatsMapper.selectPage(movieStatsPage, null);

        List<MovieDetails> movieDetailsList = movieStatsPage.getRecords().stream().map(movieStats -> {
            Movie movie = movieMapper.selectById(movieStats.getMovieId());
            MovieDetails movieDetails = new MovieDetails();
            movieDetails.setMovie(movie);
            movieDetails.setMovieStats(movieStats);
            return movieDetails;
        }).collect(Collectors.toList());

        Page<MovieDetails> resultPage = new Page<>(page, size, movieStatsPage.getTotal());
        resultPage.setRecords(movieDetailsList);
        return resultPage;
    }
}
